﻿
using Catastrophe.BasePC.UI.SystemIni.BaseData;
using Catastrophe.BasePC.UI.UIMode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace Catastrophe.BasePC.UI.UI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow :Window
    {
        /// <summary>
        /// 鼠标是否按下
        /// </summary>
        private bool isMouseDown { get; set; }

        /// <summary>
        /// 鼠标按下的位置
        /// </summary>
        Point mouseDownPoint { get; set; }

        /// <summary>
        /// 
        /// </summary>
        MainWindowModel mode { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            this.mode = this.DataContext as MainWindowModel;
            mode.UserControlLoad = new Action<UserControl>(UserControlLoadMethod);
        }

        private void UserControlLoadMethod(UserControl control)
        {
            if (control==null)
            {
                MessageBox.Show("查找窗口失败");
                return;
            }
            this.Dispatcher.BeginInvoke(new Action(() => {
                showUc.Children.Clear();
                showUc.Children.Add(control);
            }));
        }

        private void StackPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseDownPoint = e.GetPosition(this);
        }

        private void StackPanel_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton==MouseButtonState.Pressed&&mouseDownPoint!=e.GetPosition(this))
            {
                isMouseDown = true;
                //DragMove();
            }
        }

        private void StackPanel_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (isMouseDown)
            {
                isMouseDown = false;
                e.Handled = true;
            }
        }
        void Minimized(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(sender as FrameworkElement).WindowState = WindowState.Minimized;
        }
        void Normal(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(sender as FrameworkElement).WindowState = WindowState.Normal;
        }
        void Maximized(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(sender as FrameworkElement).WindowState = WindowState.Maximized;
        }
        void Close(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(sender as FrameworkElement).Close();
            System.Environment.Exit(0);
        }
        /// <summary>
        /// 菜单管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Open_Click(object sender, RoutedEventArgs e)
        {
            mode.loadForm.Execute(DataIniEntity.name_Menu);
        }
        /// <summary>
        /// 系统配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FileConfig_Click(object sender, RoutedEventArgs e)
        {
            mode.loadForm.Execute(DataIniEntity.name_FileConfig);
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            MainWindowLeft mainWindowLeft =e.NewValue as MainWindowLeft;
            if (mainWindowLeft.Nodes != null&& mainWindowLeft.Nodes.Count>0)
            {
                return;
            }
            mode.loadForm.Execute(mainWindowLeft.Entity.ResourceName);
            mode.Leftnode_Clicked(mainWindowLeft.Entity);
        }
    }
}
